# -*- coding: utf-8 -*-

from typing import Dict, List, Literal, Tuple, Union

from PIL import Image, ImageDraw, ImageFont


def get_text_Image_from_font_type(text_: str, font_: ImageFont.FreeTypeFont):
    """
    获取文字的图形
    """
    if not text_:
        raise ValueError("图形文字参数为空。")

    canvas = Image.new("RGBA", (font_.size + 1, font_.size + 1), (255, 255, 255))
    ImageDraw.Draw(canvas).text(
        xy=(0, 0),
        text=text_,
        font=font_,
        fill=(0, 0, 0),
    )
    bbox = canvas.getbbox()
    if bbox is None:
        return Image.new("RGBA", (0, 0))
    return canvas.crop(bbox)


def get_text_Image(text_: str, font_: str, font_size: int = 32):
    """
    获取文字的图形
    """
    if not text_:
        raise ValueError("图形文字参数为空。")

    canvas = Image.new("RGBA", (font_size + 1, font_size + 1))
    ImageDraw.Draw(canvas).text(
        xy=(0, 0),
        text=text_,
        font=ImageFont.truetype(
            font_,
            font_size,
        ),
        fill=(255, 255, 255),
    )
    bbox = canvas.getbbox()
    if bbox is None:
        return Image.new("RGBA", (0, 0))
    return canvas.crop(bbox)
